package com.newer.SelectorGenerator;

public class SelectorBean {
	private static final String XML_HEAD = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
	private static final String SELECTOR_HEAD = "<selector xmlns:android=\"http://schemas.android.com/apk/res/android\">";
	private static final String SELECTOR_TAIL = "</selector>";

	private static final String TEMP_DISABLE = "<item android:state_enabled=\"false\" android:drawable=\"@drawable/%1s\" />";
	private static final String TEMP_CLICK = "<item android:state_enabled=\"true\" android:state_pressed=\"true\" android:drawable=\"@drawable/%1s\" />";
	private static final String TEMP_NORMAL = "<item android:drawable=\"@drawable/%1s\" />";

	private String mainName;
	
	private String diableFileName;
	private String normalFileName;
	private String clickFileName;

	public String toXML() throws Exception {
		if(!isAvailable()){
			throw new Exception("至少要有一个文件名!");
		}
		StringBuilder sb = new StringBuilder(XML_HEAD);
		sb.append("\n");
		sb.append(SELECTOR_HEAD);
		sb.append("\n");
		if (Tools.isNotEmpty(diableFileName)) {
			sb.append(String.format(TEMP_DISABLE, diableFileName));
			sb.append("\n");
		}
		if (Tools.isNotEmpty(clickFileName)) {
			sb.append(String.format(TEMP_CLICK, clickFileName));
			sb.append("\n");
		}
		if (Tools.isNotEmpty(normalFileName)) {
			sb.append(String.format(TEMP_NORMAL, normalFileName));
			sb.append("\n");
		}
		sb.append(SELECTOR_TAIL);
		return sb.toString();
	}

	public String getDiableFileName() {
		return diableFileName;
	}

	public void setDiableFileName(String diableFileName) {
		this.diableFileName = diableFileName;
	}

	public String getNormalFileName() {
		return normalFileName;
	}

	public void setNormalFileName(String normalFileName) {
		this.normalFileName = normalFileName;
	}

	public String getClickFileName() {
		return clickFileName;
	}

	public void setClickFileName(String clickFileName) {
		this.clickFileName = clickFileName;
	}


	public String getMainName() {
		return mainName;
	}

	public void setMainName(String mainName) {
		this.mainName = mainName;
	}

	public SelectorBean(String mainName) {
		this(mainName,null,null,null);
	}
	/** 是否可以输出,至少包含两个文件名才合格*/
	public boolean isAvailable(){
		int emptyStrNum=0;
		if(Tools.isEmpty(clickFileName)){
			emptyStrNum++;
		}
		if(Tools.isEmpty(normalFileName)){
			emptyStrNum++;
		}
		if(Tools.isEmpty(diableFileName)){
			emptyStrNum++;
		}
		return emptyStrNum<2;
	}

	public SelectorBean(String mainName, String diableFileName, String normalFileName, String clickFileName) {
		super();
		this.mainName = mainName;
		this.diableFileName = diableFileName;
		this.normalFileName = normalFileName;
		this.clickFileName = clickFileName;
	}

}
